<?php
/**
 *
 * @author deathemperor
 * @version $Id: GraphVNGValidator.php 4906 2012-11-21 11:39:26Z locth $
 */

/**
 * DisableLayout Action Helper
 *
 * @uses actionHelper Zend_Controller_Action_Helper
 */
class SGN_Helper_GraphVNGValidator extends Zend_Controller_Action_Helper_Abstract
{
    /**
     *
     * @var Zend_Loader_PluginLoader
     */
    public $pluginLoader;

    /**
     * Constructor: initialize plugin loader
     *
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * Strategy pattern: call helper as broker method
     * 
     * @param array $appInfo
     * 
     * @throws Exception
     */
    public function direct($appInfo)
    {
        $gameModel = Application_Model_SGN_GameCenter::factory();
        $game = $gameModel->getGame($appInfo['appName']);
        if(!$game) {
            throw new Exception("Provided appname \"$appInfo[appName]\" does not exist.", SGN_ECD::API_APP_INVALID);
        }
        if(!$game['internalVNG'] && $appInfo['apiKey'] !== $game['apiKey'] && $appInfo['secretKey'] !== $game['secretKey']) {
            if($appInfo['apiKey'] !== $game['apiKey'] ) {
                throw new Exception("Provided apiKey ($appInfo[apiKey]) is invalid for appname ($appInfo[appName]).", SGN_ECD::API_APIKEY_INVALID);
            } else if($appInfo['secretKey'] !== $game['secretKey']) {
                throw new Exception("Provided secretKey ($appInfo[secretKey]) is invalid for appname ($appInfo[appName]).", SGN_ECD::API_SECRETKEY_INVALID);
            } else {
                throw new Exception("Access is invalid because game is external VNG.", SGN_ECD::API_APP_INVALID);
            }
        }
    }
}
